//
//  LJShowBigPictureViewController.m
//  百思不得姐
//
//  Created by 刘鹿杰 on 16/9/4.
//  Copyright © 2016年 刘鹿杰. All rights reserved.
//

#import "LJShowBigPictureViewController.h"
#import "LJTopicsModel.h"
#import "UIView+LJExtension.h"
#import <UIImageView+WebCache.h>
#import "LJCircularProgressView.h"
#import <SVProgressHUD.h>


@interface LJShowBigPictureViewController ()

// scrollView  可以滚动
@property (weak, nonatomic) IBOutlet UIScrollView *pictureScrollView;
/** progressView 进度条控件 */
@property (weak, nonatomic) IBOutlet LJCircularProgressView *circularProgressView;
/** UIImageView*/
@property (nonatomic, weak) UIImageView *pictureImageView;


@end

@implementation LJShowBigPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIImageView * pictureImageView = [[UIImageView alloc] init];
    pictureImageView.userInteractionEnabled = YES;
    [pictureImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backBtnClick:)]];
    
    [self.pictureScrollView addSubview:pictureImageView];
    
    self.pictureImageView = pictureImageView;
    
    
    // 控制图片的尺寸
    CGFloat pictureW = KScreenW;
    CGFloat pictureH = pictureW * self.topicsModel.height / self.topicsModel.width;

    if (pictureH > KScreenH) {
        pictureImageView.frame = CGRectMake(0, 0, pictureW, pictureH);
        self.pictureScrollView.contentSize = CGSizeMake(pictureW, pictureH);
    }else{
        pictureImageView.size = CGSizeMake(pictureW, pictureH);
        pictureImageView.centery = KScreenH * 0.5;
    }
    
    // 下载图片
    [self.circularProgressView setProgress:self.topicsModel.pictureProgress animated:YES];
    
    
    [pictureImageView sd_setImageWithURL:[NSURL URLWithString:self.topicsModel.big_image0] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        self.topicsModel.pictureProgress = 1.0 * receivedSize / expectedSize;
        
        [self.circularProgressView setProgress:self.topicsModel.pictureProgress animated:NO];
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.circularProgressView.hidden = YES;
    }];
    
    
}

- (IBAction)saveBtnClick:(id)sender {
    NSLog(@"保存");
    
    if (self.pictureImageView.image == nil) {
        [SVProgressHUD showErrorWithStatus:@"图片并没下载完毕!"];
        return;
    }
    // 将图片写入相册
    UIImageWriteToSavedPhotosAlbum(self.pictureImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败!"];
    } else {
        [SVProgressHUD showSuccessWithStatus:@"保存成功!"];
    }
}


- (IBAction)forwardImage:(id)sender {
    NSLog(@"转发");
}


- (IBAction)backBtnClick:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}




@end
